# -*- coding: utf-8 -*-

import time
import logging
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, WebDriverException
from webdriver_manager.chrome import ChromeDriverManager
from scrapy.http import HtmlResponse

# 设置 Selenium 日志级别为 ERROR，减少输出
logging.getLogger('selenium').setLevel(logging.ERROR)
logging.getLogger('urllib3').setLevel(logging.ERROR)
logging.getLogger('webdriver_manager').setLevel(logging.ERROR)

class SeleniumRenderer:
    """Selenium headless 渲染器"""
    
    def __init__(self, headless=True, timeout=30):
        self.headless = headless
        self.timeout = timeout
        self.logger = logging.getLogger(__name__)
        self.driver = None
    
    def setup_driver(self):
        """设置 Chrome WebDriver"""
        options = Options()
        
        if self.headless:
            options.add_argument('--headless')
        
        # 基本设置
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-dev-shm-usage')
        options.add_argument('--disable-gpu')
        options.add_argument('--disable-web-security')
        options.add_argument('--disable-features=VizDisplayCompositor')
        
        # 用户代理
        options.add_argument('--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
        
        # 窗口大小
        options.add_argument('--window-size=1920,1080')
        
        # 禁用日志输出
        options.add_argument('--log-level=3')
        options.add_argument('--silent')
        
        # 设置代理
        self._setup_proxy(options)
        
        # 禁用图片加载以提高速度（可选）
        # prefs = {"profile.managed_default_content_settings.images": 2}
        # options.add_experimental_option("prefs", prefs)
        
        try:
            # 使用 webdriver-manager 自动管理 ChromeDriver
            service = Service(ChromeDriverManager().install())
            self.driver = webdriver.Chrome(service=service, options=options)
            self.driver.set_page_load_timeout(self.timeout)
            return True
        except Exception as e:
            self.logger.error(f"Chrome WebDriver 初始化失败: {e}")
            return False
    
    def _setup_proxy(self, options):
        """设置代理"""
        # 读取环境变量中的代理设置
        https_proxy = os.environ.get('https_proxy')
        http_proxy = os.environ.get('http_proxy')
        all_proxy = os.environ.get('all_proxy')
        
        # 优先使用 https_proxy，然后是 http_proxy，最后是 all_proxy
        proxy = https_proxy or http_proxy or all_proxy
        
        if proxy:
            self.logger.info(f"使用代理: {proxy}")
            options.add_argument(f'--proxy-server={proxy}')
        else:
            self.logger.info("未设置代理")
    
    def render_page(self, url, wait_time=3):
        """渲染页面并返回 HTML"""
        if not self.driver:
            if not self.setup_driver():
                return None
        
        try:
            self.driver.get(url)
            
            # 等待页面加载
            time.sleep(wait_time)
            
            # 等待页面主要内容加载
            try:
                WebDriverWait(self.driver, 10).until(
                    EC.presence_of_element_located((By.TAG_NAME, "body"))
                )
            except TimeoutException:
                pass  # 静默处理超时
            
            # 获取渲染后的 HTML
            html = self.driver.page_source
            return html
            
        except Exception as e:
            self.logger.error(f"页面渲染失败: {e}")
            return None
    
    def create_response(self, url, html, meta=None):
        """创建 Scrapy HtmlResponse"""
        if html:
            from scrapy import Request
            # 创建一个虚拟的 request 对象，传递 meta
            request = Request(url=url, meta=meta or {})
            
            response = HtmlResponse(
                url=url,
                body=html.encode('utf-8'),
                encoding='utf-8',
                request=request  # 关联 request 对象
            )
            return response
        return None
    
    def close(self):
        """关闭 WebDriver"""
        if self.driver:
            try:
                self.driver.quit()
            except Exception as e:
                self.logger.error(f"关闭 WebDriver 失败: {e}")
            finally:
                self.driver = None

# 全局渲染器实例
_selenium_renderer = None

def get_selenium_renderer():
    """获取全局 Selenium 渲染器实例"""
    global _selenium_renderer
    if _selenium_renderer is None:
        _selenium_renderer = SeleniumRenderer(headless=True)
    return _selenium_renderer

def close_selenium_renderer():
    """关闭全局 Selenium 渲染器"""
    global _selenium_renderer
    if _selenium_renderer:
        _selenium_renderer.close()
        _selenium_renderer = None 